package pojo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static db4o.DBUtils.getMaxIdOfDevice;

public class Device {

	private Integer id;
	private String description;
	private EDeviceType type;
	private EDeviceState state;
	private Date date;
	private int clientId;
	private int servicemanId;

	public int getClientId() {
		return clientId;
	}

	public void setClientId(int clientId) {
		this.clientId = clientId;
	}

	public int getServicemanId() {
		return servicemanId;
	}

	public void setServicemanId(int servicemanId) {
		this.servicemanId = servicemanId;
	}

	private List<User> users = new ArrayList<User>();

	public Device() {
	}

	public Device(Integer id, String description, EDeviceType type) {
		super();
		this.id = id;
		this.description = description;
		this.type = type;
	}

	public Device(Integer id, String description, EDeviceType type,
			EDeviceState state, Date date, int clientId, int servicemanId) {
		super();
		this.id = id;
		this.description = description;
		this.type = type;
		this.state = state;
		this.date = date;
		this.servicemanId = servicemanId;
		this.clientId = clientId;
	}

	public Device(String description, EDeviceType type) {
		super();
		this.description = description;
		this.type = type;
		setDate(new Date());
		setState(EDeviceState.WAITING);
		setId(getMaxIdOfDevice() + 1);
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void addUser(User u) {
		users.add(u);
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public EDeviceState getState() {
		return state;
	}

	public void setState(EDeviceState state) {
		this.state = state;
	}

	public EDeviceType getType() {
		return type;
	}

	public void setType(EDeviceType type) {
		this.type = type;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((state == null) ? 0 : state.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		result = prime * result + ((users == null) ? 0 : users.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Device other = (Device) obj;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (state != other.state)
			return false;
		if (type != other.type)
			return false;
		if (users == null) {
			if (other.users != null)
				return false;
		} else if (!users.equals(other.users))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Device [id=" + id + ", description=" + description + ", type="
				+ type + ", state=" + state + ", date=" + date + ", clientId="
				+ clientId + ", servicemanId=" + servicemanId + ", users="
				+ users + "]";
	}

}
